import 'package:flutter_riverpod/flutter_riverpod.dart';

/// 页面状态枚举
enum PageStatus {
  idle, // 空闲状态
  loading, // 加载中
  success, // 成功
  empty, // 空数据
  error, // 错误
}

/// 基础状态类
class BaseState<T> {
  final PageStatus status;
  final T? data;
  final String? errorMessage;
  final Exception? exception;

  const BaseState({
    this.status = PageStatus.idle,
    this.data,
    this.errorMessage,
    this.exception,
  });

  /// 创建加载状态
  BaseState<T> loading() {
    return BaseState<T>(
      status: PageStatus.loading,
      data: data,
    );
  }

  /// 创建成功状态
  BaseState<T> success(T newData) {
    return BaseState<T>(
      status: PageStatus.success,
      data: newData,
    );
  }

  /// 创建空数据状态
  BaseState<T> empty() {
    return BaseState<T>(
      status: PageStatus.empty,
      data: data,
    );
  }

  /// 创建错误状态
  BaseState<T> error(String message, [Exception? exception]) {
    return BaseState<T>(
      status: PageStatus.error,
      data: data,
      errorMessage: message,
      exception: exception,
    );
  }

  /// 是否正在加载
  bool get isLoading => status == PageStatus.loading;

  /// 是否成功
  bool get isSuccess => status == PageStatus.success;

  /// 是否为空
  bool get isEmpty => status == PageStatus.empty;

  /// 是否有错误
  bool get hasError => status == PageStatus.error;

  /// 是否有数据
  bool get hasData => data != null;

  @override
  String toString() {
    return 'BaseState(status: $status, data: $data, errorMessage: $errorMessage)';
  }
}

/// 基础通知器
abstract class BaseNotifier<T> extends StateNotifier<BaseState<T>> {
  BaseNotifier() : super(const BaseState());

  /// 设置加载状态
  void setLoading() {
    if (mounted) {
      state = state.loading();
    }
  }

  /// 设置成功状态
  void setSuccess(T data) {
    if (mounted) {
      state = state.success(data);
    }
  }

  /// 设置空数据状态
  void setEmpty() {
    if (mounted) {
      state = state.empty();
    }
  }

  /// 设置错误状态
  void setError(String message, [Exception? exception]) {
    if (mounted) {
      state = state.error(message, exception);
    }
  }

  /// 异步执行操作
  Future<void> execute(Future<T> Function() operation) async {
    try {
      setLoading();
      final result = await operation();
      if (result == null || (result is List && result.isEmpty)) {
        setEmpty();
      } else {
        setSuccess(result);
      }
    } catch (e) {
      setError(e.toString(), e is Exception ? e : Exception(e.toString()));
    }
  }

  /// 刷新数据
  Future<void> refresh() async {
    await loadData();
  }

  /// 加载数据 - 子类实现
  Future<void> loadData();
}

/// 基础异步通知器
abstract class BaseAsyncNotifier<T> extends AsyncNotifier<T> {
  @override
  Future<T> build() async {
    return await loadData();
  }

  /// 加载数据 - 子类实现
  Future<T> loadData();

  /// 刷新数据
  Future<void> refresh() async {
    state = const AsyncValue.loading();
    try {
      final data = await loadData();
      state = AsyncValue.data(data);
    } catch (error, stackTrace) {
      state = AsyncValue.error(error, stackTrace);
    }
  }

  /// 执行操作
  Future<void> execute(Future<T> Function() operation) async {
    state = const AsyncValue.loading();
    try {
      final result = await operation();
      state = AsyncValue.data(result);
    } catch (error, stackTrace) {
      state = AsyncValue.error(error, stackTrace);
    }
  }
}
